package com.example.kaogong.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.kaogong.common.exception.ParamsErrorException;
import com.example.kaogong.common.utils.Assert;
import com.example.kaogong.common.utils.RespBean;
import com.example.kaogong.sys.pojo.dto.PaperCheckDto;
import com.example.kaogong.sys.pojo.dto.StudentPaperDto;
import com.example.kaogong.sys.pojo.vo.StudentPaperDetailVo;
import com.example.kaogong.sys.pojo.vo.StudentPaperInfoVo;
import com.example.kaogong.sys.service.IStudentPaperService;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

/**
 * <p>
 * 学生试卷 前端控制器
 * </p>
 *
 * @author    
 * @since 2022-03-31
 */
@RestController
@RequestMapping("/student-paper")
@Api(tags = "学生试卷接口")
@Slf4j
public class StudentPaperController {

    @Autowired
    private IStudentPaperService studentPaperService;

    @ApiOperation(value = "当前学生考试试卷提交")
    @PostMapping(value = "/submit")
    public RespBean paperSubmit(@RequestBody StudentPaperDto studentPaperDto, Principal principal) {
        if (principal == null)
            return RespBean.error();
        Assert.notNull(principal.getName(), new ParamsErrorException("用户名不能为空"));
        log.info("request:{}", new Gson().toJson(studentPaperDto));
        RespBean respBean = studentPaperService.paperSubmit(studentPaperDto, principal.getName());
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "教师批阅学生试卷")
    @PostMapping(value = "/check")
    public RespBean studentPaperCheck(@RequestBody PaperCheckDto paperCheckDto) {
        log.info("request:{}", new Gson().toJson(paperCheckDto));
        RespBean respBean = studentPaperService.studentPaperCheck(paperCheckDto);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "当前登录教师获取所命题试卷所有未批阅的试卷基本信息")
    @GetMapping(value = "/unchecked/paper")
    public RespBean<List<StudentPaperInfoVo>> getAllUncheckedPaper(Principal principal) {
        if (principal == null)
            return RespBean.error();
        Assert.notNull(principal.getName(), new ParamsErrorException("用户名不能为空"));
        RespBean<List<StudentPaperInfoVo>> respBean = studentPaperService.getAllUncheckedPaper(principal.getName());
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "当前登录学生获取所有作答试卷的基本信息")
    @GetMapping(value = "/student/all")
    public RespBean<List<StudentPaperInfoVo>> getStudentAllStudentPaper(Principal principal) {
        if (principal == null)
            return RespBean.error();
        Assert.notNull(principal.getName(), new ParamsErrorException("用户名不能为空"));
        RespBean<List<StudentPaperInfoVo>> respBean = studentPaperService.getStudentAllStudentPaper(principal.getName());
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "根据作答试卷编号获取试卷详细作答信息")
    @GetMapping(value = "/detail/{id}")
    public RespBean<StudentPaperDetailVo> getStudentPaperDetailInfo(@PathVariable Integer id) {
        Assert.notNull(id, new ParamsErrorException("编号不能为空"));
        log.info("id:{}", id);
        RespBean<StudentPaperDetailVo> respBean = studentPaperService.getStudentPaperDetailInfo(id);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }
}
